import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

import '../../utils/http.dart';
import '../../utils/toast.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  Map notifyDetail = {};

  Map<String, dynamic> params = {};

// 更新 生命周期
// 触发时机有2种 ：1 首次创建组件  2 接收外部数据发生了变化
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final data = ModalRoute.of(context)?.settings.arguments;
    if (data != null) {
      params = data as Map<String, dynamic>;
      // 重新加载新的公共详情
      getNotifyDetail();
    }
  }

  void getNotifyDetail() async {
    try {
      // 请求数据 /goods/100
      var res = await http.get('/announcement/${params['id']}');
      // code = 10000 表示请求正常
      if (res['code'] != 10000) return ToastUtil.showError('获取数据失败');
      ToastUtil.showSuccess('获取数据成功');
      setState(() {
        notifyDetail = res['data'];
      });
    } catch (e) {
      ToastUtil.showError('请求失败：$e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('公告详情123'),
        ),
        body: ListView(children: [
          Container(
              padding: const EdgeInsets.all(10),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // 标题
                    Text(notifyDetail['title'] ?? '',
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        )),
                    const SizedBox(height: 10),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(notifyDetail['creatorName'] ?? '',
                              style: const TextStyle(color: Colors.grey)),
                          Text(notifyDetail['createdAt'] ?? '',
                              style: const TextStyle(color: Colors.grey))
                        ]),
                    const SizedBox(height: 10),
                    // 内容
                    Html(
                      //  ??  空值合并操作符  左边的如果是null或者undefined 就返回右边的结果
                      data: notifyDetail['content'] ?? '',
                    )
                  ]))
        ]));
  }
}
